package com.luntan.school.dao.impl;

import com.luntan.school.dao.CommentDao;
import com.luntan.school.model.Comment;
import com.luntan.school.model.User;
import com.luntan.school.util.CommentUtil;
import com.luntan.school.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CommentDaolmpl implements CommentDao {

    String ADD_SQL = "insert into comment(content,pubdate,hidden,likeCount,dislikeCount,reply_for_id,creator,posts_id) values(?,?,?,?,?,?,?,?)";

    @Override
    public void addComment(Comment comment) {
        try {
            // 获取连接
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(ADD_SQL);
            preparedStatement.setString(1, comment.getContent());
            preparedStatement.setTimestamp(2, new Timestamp(new Date().getTime()));
            preparedStatement.setBoolean(3, false); // 默认不隐藏
            preparedStatement.setInt(4, 0);
            preparedStatement.setInt(5, 0);
            if (comment.getReplyFor() != null) {
                preparedStatement.setLong(6, comment.getReplyFor().getId());
            } else {
                preparedStatement.setNull(6, Types.BIGINT); // 被回复的评论为空(即新评论)
            }
            preparedStatement.setInt(7, comment.getCreator().getId());
            preparedStatement.setInt(8, comment.getPosts().getId());
            preparedStatement.executeUpdate();
            preparedStatement.close();
            connection.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Comment> getCommentsByPostsId(int postsId) {
        List<Comment> list = new ArrayList<>();
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT c.*, r.id AS rid, u.nickname, u.photo \n" +
                    "FROM `comment` AS c " +
                    "LEFT JOIN `comment` AS r ON r.id = c.reply_for_id " +
                    "LEFT JOIN `user` AS u ON c.creator = u.id " +
                    "WHERE c.posts_id=?");
            preparedStatement.setInt(1, postsId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Comment comment = new Comment();
                comment.setId(resultSet.getLong("id"));
                comment.setContent(resultSet.getString("content"));
                comment.setLikeCount(resultSet.getInt("likeCount"));
                comment.setDislikeCount(resultSet.getInt("dislikeCount"));

                Comment replyFor = new Comment();
                long replyForId = resultSet.getLong("reply_for_id");
                if (!resultSet.wasNull()) {
                    replyFor.setId(replyForId);
                }
                comment.setReplyFor(replyFor);

                User creator = new User();
                creator.setId(resultSet.getInt("creator"));
                creator.setNickname(resultSet.getString("nickname"));
                creator.setPhoto(resultSet.getString("photo"));
                comment.setCreator(creator);

                comment.setReplyId(resultSet.getLong("rid"));
                comment.setPubdate(resultSet.getTimestamp("pubdate"));
                comment.setHidden(resultSet.getBoolean("hidden"));

                list.add(comment);
            }

            resultSet.close();
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int updateLikeNum(int commentId, boolean likeNum) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT likeCount FROM comment WHERE id = ?");
            preparedStatement.setInt(1, commentId);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                int likeCount = resultSet.getInt("likeCount");
                if (likeNum) {
                    likeCount++;
                } else if (!likeNum && likeCount > 0) {
                    likeCount--;
                }
                // 更新点赞数
                preparedStatement = connection.prepareStatement("UPDATE comment SET likeCount = ? WHERE id = ?");
                preparedStatement.setInt(1, likeCount);
                preparedStatement.setInt(2, commentId);
                preparedStatement.executeUpdate();
                resultSet.close();
                preparedStatement.close();
                connection.close();
                return likeCount;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1; // 出错时返回
    }

    @Override
    public int updateDislikeNum(int commentID, boolean dislike) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT dislikeCount FROM comment WHERE id = ?");
            preparedStatement.setInt(1, commentID);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                int dislikeCount = resultSet.getInt("dislikeCount");
                if (dislike) {
                    dislikeCount++;
                } else if (!dislike && dislikeCount > 0) {
                    dislikeCount--;
                }
                // 更新点踩数
                preparedStatement = connection.prepareStatement("UPDATE comment SET dislikeCount = ? WHERE id = ?");
                preparedStatement.setInt(1, dislikeCount);
                preparedStatement.setInt(2, commentID);
                preparedStatement.executeUpdate();
                resultSet.close();
                preparedStatement.close();
                connection.close();
                return dislikeCount;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1; // 出错时返回
    }

    @Override
    public void hiddenComment(long commentId, boolean hidden) {
        Connection connection = DBUtil.getConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("UPDATE comment SET hidden=? WHERE id=?");
            preparedStatement.setBoolean(1, hidden);
            preparedStatement.setLong(2, commentId);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        CommentDao commentDao = new CommentDaolmpl();
        //System.out.println(commentDao.getAllCommentByPostsId(562301));
        System.out.println(CommentUtil.toMap(commentDao.getCommentsByPostsId(562301)));
    }
}
